package com.freesun.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freesun.shop.entity.Notice;
import com.freesun.shop.service.NoticeService;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shop/notice")
public class NoticeController {
    @Reference(check = false)
    private NoticeService noticeService;

    @ApiOperation("公告条件分页")
    @GetMapping("/page")
    public ResponseEntity<IPage<Notice>> findByPage(Notice notice, Page<Notice> page) {
        IPage<Notice> iPage= noticeService.findByPage(notice,page);
        return ResponseEntity.ok(iPage);
    }
    @ApiOperation("单个删除公告")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id){
        //notice 在redis里面又换成,需要删除。
        noticeService.removeById(id);
        return ResponseEntity.ok().build();
    }
    @ApiOperation("查询单个公告")
    @GetMapping("/info/{id}")
    public ResponseEntity<Notice> getById(@PathVariable("id") Long id){
        Notice notice = noticeService.getById(id);
        return ResponseEntity.ok(notice);
    }
    @ApiOperation("新增公告")
    @PostMapping
    public ResponseEntity<Notice> saveNotice(@RequestBody Notice notice){
        noticeService.save(notice);
        return ResponseEntity.ok().build();
    }
    @ApiOperation("修改公告")
    @PutMapping
    public ResponseEntity<Notice> updateNotice(@RequestBody Notice notice){
        noticeService.updateById(notice);
        return ResponseEntity.ok().build();
    }
}
